package com.zbkj.crmeb.validatecode.service.impl;

import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.constants.Constants;
import com.crmeb.core.exception.CrmebException;
import com.utils.CrmebUtil;
import com.crmeb.core.utils.RedisUtil;
import com.utils.ValidateCodeUtil;
import com.zbkj.crmeb.validatecode.model.ValidateCode;
import com.zbkj.crmeb.validatecode.service.ValidateCodeService;

import cn.hutool.core.util.StrUtil;

/**
* @author Mr.Zhang
* @Description SystemAdminServiceImpl 接口实现
* @since 2020-04-13
*/
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 获取验证码信息
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public ValidateCode get() {
        ValidateCodeUtil.Validate randomCode = ValidateCodeUtil.getRandomCode();//直接调用静态方法，返回验证码对象
        if(randomCode == null){
            return null;
        }

        String value = randomCode.getValue();
        Integer result = randomCode.getResult();
        String md5Key = DigestUtils.md5Hex(value);
        String redisKey = getRedisKey(md5Key);
        redisUtil.set(redisKey, result, 5L, TimeUnit.MINUTES);   //5分钟过期
        String base64Str = randomCode.getBase64Str();
        return new ValidateCode(md5Key, CrmebUtil.getBase64Image(base64Str));
    }

    /**
     * 获取redis key
     * @param md5Key value的md5加密值
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    public String getRedisKey(String md5Key){
    	return StrUtil.format(Constants.VALIDATE_REDIS_KEY_PREFIX, md5Key);
    }

    /**
     * 验证
     * @param validateCode
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    public boolean check(ValidateCode validateCode){
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        String key = getRedisKey(validateCode.getKey());
        if(!redisUtil.exists(key) || !pattern.matcher(validateCode.getCode()).matches()) {
        	throw new CrmebException("验证码错误");
        }
        Object redisValue = redisUtil.get(key);
        if(!redisValue.toString().equals(validateCode.getCode())){
        	redisUtil.remove(key);
            return false;
        }
        return true;
    }
}

